import dayjs from "dayjs";

// 获取应用实例
const app = getApp();

// 初始化数据库
const db = wx.cloud.database();
const uimg = db.collection("uimg");

// pages/addmemorial/addmemorial.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    theme: 1,
    openid: "",
    o_openid: "",
    content: "",
    time: "",
    myList: [],
    memorList: [],
    showTime: false,
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    formatter(type, value) {
      if (type === "year") {
        return `${value}年`;
      }
      if (type === "month") {
        return `${value}月`;
      }
      return value;
    },

    navHeight: app.globalData.navBarHeight,
    menuHeight: app.globalData.menuHeight,
    menuRight: app.globalData.menuRight,
    menuTop: app.globalData.menuTop,
  },

  onInput(event) {
    let { time } = this.data;
    time = dayjs(event.detail).format("YYYY/MM/DD");
    this.setData({
      time,
    });
  },

  onCancel() {
    this.setData({
      showTime: false,
      time: "",
    });
  },

  onFinish() {
    this.setData({
      showTime: false,
    });
  },

  onFocus() {
    this.setData({
      showTime: true,
    });
  },

  onChange(e) {
    let { content } = this.data;
    content = e.detail;
    this.setData({
      content,
    });
  },

  // 获取数据
  getData() {
    const that = this;
    const { openid } = that.data;
    uimg.where({ _openid: openid }).get({
      success: (res) => {
        const myList = res.data[0].memorial;
        that.setData({
          myList,
        });
      },
    });
  },

  // 添加纪念日
  addMemorial() {
    let { content, time, openid, o_openid, myList } = this.data;

    if (content == "" || time == "") {
      wx.showModal({
        title: "提示",
        content: "内容和时间都是必填项哦~",
      });
      return;
    }
    myList.push({ content, time, handleTime: new Date().getTime() });

    uimg.where({ _openid: openid }).update({
      data: {
        memorial: myList,
      },
      success: () => {
        uimg.where({ _openid: o_openid }).update({
          data: {
            memorial: myList,
          },
          success: () => {
            wx.showToast({
              title: "添加成功",
              icon: "success",
            });
          },
          fail: () => {
            wx.showToast({
              title: "添加失败",
              icon: "error",
            });
          },
          complete: () => {
            wx.navigateBack({
              delta: 1,
            });
          },
        });
      },
      fail: () => {
        wx.showToast({
          title: "添加失败",
          icon: "error",
        });
      },
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      theme: app.globalData.theme,
      openid: wx.getStorageSync("openid"),
      o_openid: wx.getStorageSync("o_openid"),
    });
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
